package com.izhangqian.signproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.izhangqian.signproject.R;
import com.izhangqian.signproject.beans.PluginBean;

import java.util.ArrayList;
import java.util.List;

public class HomePluginAdapter extends RecyclerView.Adapter<HomePluginAdapter.PluginViewHolder> {

    private List<PluginBean> mBeans = new ArrayList<>();
    private Context mContext;

    public HomePluginAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public PluginViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.home_plugin_item_layout, null);
        return new PluginViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PluginViewHolder holder, int position) {
        holder.bindView(mBeans.get(position));
    }

    public void updateList(List<PluginBean> beans) {
        if (beans != null) {
            mBeans.clear();
            mBeans.addAll(beans);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return mBeans.size();
    }

    public static class PluginViewHolder extends RecyclerView.ViewHolder {

        public TextView name;
        public PluginViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.plugin_name);
        }

        public void bindView(PluginBean bean) {
            name.setText(bean.name);
        }
    }
}
